<?php if (!defined('BASEPATH')) exit ('No direct script access allowed');
class Barang extends Member_Controller
{
	function __construct()
	{
		parent::__construct();
		$this->load->model('data_barang_model','',TRUE);
	}
	
	function index()
	{
		$partial['_header']='template/header';
		$partial['_sidebar']="template/sidebar";
		$partial['_contents']="barang/barang";
		$data['data'] = $this->data_barang_model->getAll()->result();
		$this->template->load('index',$partial,$data);
	}
	function barang2()
	{
		$partial['_header']='template/header';
		$partial['_sidebar']="template/sidebar";
		$partial['_contents']="barang/barang2";
		$data['data'] = $this->data_barang_model->getAll()->result();
		$this->template->load('index',$partial,$data);
	}
	function add()
	{
		$partial['_header']='template/header';
		$partial['_sidebar']="template/sidebar";
		$partial['_contents']="barang/form_barang";
		$get_id = "";
		$data['kategori'] = $this->data_barang_model->getKategori($get_id)->result();
		$data['readonly']="";
		$data['data']['id_barang']='';
		$data['data']['nama_barang']='';
		$data['data']['id_kategori_barang']='';
		$this->load->model('data_gudang_model','',TRUE);
		$data['nama_bahan_gudang']=$this->data_gudang_model->getName()->result();
		$this->template->load('index',$partial,$data);
	}
	
	function update($id)
	{
		$partial['_header']='template/header';
		$partial['_sidebar']="template/sidebar";
		$partial['_contents']="barang/form_barang";
		$data['data'] = $this->data_barang_model->get_by_id($id)->row_array();
		$get_id = $this->data_barang_model->get_by_id($id)->row();
		$data['kategori'] = $this->data_barang_model->getKategori($get_id->id_kategori_barang)->result();
		$data['langkah'] = $this->data_barang_model->get_langkah_by_id($id)->result();
		$data['kebutuhan'] = $this->data_barang_model->get_bahan_by_id($id)->result();
		$data['readonly']="readonly";
		$this->template->load('index',$partial,$data);
	}
	
	function delete($id)
	{
		$this->data_gudang_model->delete($id);
		redirect('bahan');
	}
	function tes(){
		echo $_POST['value'].' (server updated)';
	}
	function action()
	{
//		print_r($_POST);
//		echo count($_POST['nama_bahan']);
//		die();

		$this->load->model('data_gudang_model','',TRUE);
		
		if (isset($_POST['add'])){
			//insert barang
			$barang=array(
			'id_barang'=>$this->input->post('id_barang'),
			'nama_barang'=>$this->input->post('nama_barang'),
			'id_kategori_barang'=>$this->input->post('kategori'));
			$this->data_barang_model->save($barang);
			//insert kebutuhan
			for($i = 1; $i<count($_POST['nama_bahan']);$i++){
			$id_bahan = $this->data_gudang_model->get_id_bahan($_POST['nama_bahan'][$i])->row();
			$kebutuhan=array(
				'jumlah_kebutuhan'=>$_POST['jumlah'][$i],
				'id_barang'=>$this->input->post('id_barang'),
				'id_bahan'=>$id_bahan->id_bahan);
			$this->data_barang_model->save_kebutuhan($kebutuhan);
			}

			//insert langkah
			for($i = 0; $i<count($_POST['langkah']);$i++){
			$langkah=array(
				'nama_langkah_kerja'=>$_POST['langkah'][$i],
				'id_barang'=>$this->input->post('id_barang'));
				$this->data_barang_model->save_langkah($langkah);
				}
			}
		else if (isset($_POST['update'])){
		$id = $this->input->post('id_bahan');
		$bahan=array(
			'nama_bahan'=>$this->input->post('nama_bahan'),
			'stok'=>$this->input->post('stok'),
			'harga'=>$this->input->post('harga'),
			'id_satuan'=>$this->input->post('satuan'),
			'id_kategori_bahan'=>$this->input->post('kategori'));
			//$this->data_gudang_model->update($id,$bahan);
		}
		redirect('barang');
	}
}